package com.liner.homework;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Homework01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        /*1.Addition随机加法运算器
        要求:由系统随机出10道加法题，而后由用户来答题，答题后输出"答对了"或"答错了"，答对1题得10分，答错1题不扣分，最后输出总分数。
       */
        System.out.println("第1题:");
        //第1遍
        int num1, num2;
        int score = 0;
        System.out.println("两位数加法计算:");
        for (int i = 1; i <= 10; i++) {
            num1 = random.nextInt(100);
            num2 = random.nextInt(100);
            System.out.println("第" + i + "题为:" + num1 + " + " + num2 + " = ");
            System.out.print("请输入答案:");
            int answer = scanner.nextInt();
            if (num1 + num2 == answer) {
                System.out.println("答对了");
                score += 10;
            } else {
                System.out.println("答错了");
            }
        }
        System.out.println("最终得分：" + score);


        //第2遍
        System.out.println("两位数加法考一考:");
        for (int i = 1; i <= 10; i++) {
            num1 = (int) (Math.random() * 100);
            num2 = (int) (Math.random() * 100);
            System.out.println("第" + i + "题:" + num1 + " + " + num2 + " = ");
            int result = num1 + num2;
            System.out.print("请输入答案:");
            int answer = scanner.nextInt();
            if (result == answer) {
                System.out.println("答对了");
                score += 10;
            } else {
                System.out.println("答错了");
            }
        }
        System.out.println("最终得分：" + score);


        /*
        * 2. MultiTable九九乘法表
        要求:输出九九乘法表
        * */
        System.out.println("第2题:");

        //第1遍
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " * " + j + " = " + i * j + "\t");
            }
            System.out.println();
        }

        //第2遍
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " * " + j + " = " + i * j + "\t");
            }
            System.out.println();
        }


        /*
         *  3.数组小代码练习:声明、初始化、访问、遍历
         * */

        System.out.println("第3题:");

        //第1遍
        int[] ints = new int[12];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = random.nextInt(100);
        }
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i] + "\t");
        }

        System.out.println();

        //第2遍
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100);
        }
        for (int k : array) {
            System.out.print(k + "\t");
        }


        /*
         * 4.MaxOfArray求数组元素最大值
         * */
        System.out.println("\n第4题:");

        //此题目为对 3. 题的数组元素最大值练习
        //第1遍
        int max = 0;
        for (int i = 0; i < ints.length; i++) {
            if (ints[i] > max) {
                max = ints[i];
            }
        }
        System.out.println("max: " + max);

        //第2遍
        max = 0;
        for (int value : array) {
            if (value > max) {
                max = value;
            }
        }
        System.out.println("max: " + max);

        /*
         *  5.对数组进行升序排列，并输出排序后的结果
         * */
        System.out.println("第5题:");

        //此题目为对 3. 题的升序排序练习
        //第1遍

        System.out.println("\n数组排序前：");
        for (int j : ints) {
            System.out.print(j + "\t");
        }
        System.out.println("\n数组排序后：");
        Arrays.sort(ints);
        for (int j : ints) {
            System.out.print(j + "\t");
        }

        System.out.println("\n--------------------------------");

        //第2遍

        System.out.println("\n数组排序前：");
        for (int k : array) {
            System.out.print(k + "\t");
        }

        System.out.println("\n数组排序后：");
        Arrays.sort(array);
        for (int k : array) {
            System.out.print(k + "\t");
        }



    }
}
